package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqSolidliquidProduceFaultMapper;
import com.guofen.pojo.SjqSolidliquidProduceElectricity;
import com.guofen.pojo.SjqSolidliquidProduceFault;
import com.guofen.service.sjq.SjqSolidliquidProduceFaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqSolidliquidProduceFaultServiceimpl implements SjqSolidliquidProduceFaultService {
    @Autowired
    private SjqSolidliquidProduceFaultMapper sjqSolidliquidProduceFaultMapper;
    @Override
    public boolean addForm(String fillTime,String equipmentName, String faultTime, String operator, String faultContent, String serviceTimeStart, String serviceTimeEnd, String servicemanInCharge, String serviceMan, String faultReason, String serviceMethod, String proposer, String recoverTime, String inspector) {
        SjqSolidliquidProduceFault sjqSolidliquidProduceFault = new SjqSolidliquidProduceFault();
        sjqSolidliquidProduceFault.setFillTime(fillTime);
        sjqSolidliquidProduceFault.setEquipmentName(equipmentName);
        sjqSolidliquidProduceFault.setFaultTime(faultTime);
        sjqSolidliquidProduceFault.setOperator(operator);
        sjqSolidliquidProduceFault.setFaultContent(faultContent);
        sjqSolidliquidProduceFault.setServiceTimeStart(serviceTimeStart);
        sjqSolidliquidProduceFault.setServiceTimeEnd(serviceTimeEnd);
        sjqSolidliquidProduceFault.setServicemanInCharge(servicemanInCharge);
        sjqSolidliquidProduceFault.setServiceman(serviceMan);
        sjqSolidliquidProduceFault.setFaultReason(faultReason);
        sjqSolidliquidProduceFault.setServiceMethod(serviceMethod);
        sjqSolidliquidProduceFault.setProposer(proposer);
        sjqSolidliquidProduceFault.setRecoverTime(recoverTime);
        sjqSolidliquidProduceFault.setInspector(inspector);
        int result = sjqSolidliquidProduceFaultMapper.insert(sjqSolidliquidProduceFault);
        System.out.println(result);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public SjqSolidliquidProduceFault findForm() {
        return sjqSolidliquidProduceFaultMapper.getLast();
    }

    @Override
    public SjqSolidliquidProduceFault getLastByEquipmentName(String equipmentName) {
        return sjqSolidliquidProduceFaultMapper.getLastByEquipmentName(equipmentName);
    }

    @Override
    public boolean addFrontPart(String equipmentName, String faultTime, String operator, String faultContent) {
        SjqSolidliquidProduceFault sjqSolidliquidProduceFault = new SjqSolidliquidProduceFault();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        sjqSolidliquidProduceFault.setFillTime(date);
        sjqSolidliquidProduceFault.setEquipmentName(equipmentName);
        sjqSolidliquidProduceFault.setFaultTime(faultTime);
        sjqSolidliquidProduceFault.setOperator(operator);
        sjqSolidliquidProduceFault.setFaultContent(faultContent);
        int result = sjqSolidliquidProduceFaultMapper.insertSelective(sjqSolidliquidProduceFault);
        if (result == 1)
            return true;
        return false;
    }

    @Override
    public boolean addAfterPart(Integer faultId, String serviceTimeStart, String serviceTimeEnd,
                                String servicemanInCharge, String serviceman, String faultReason, String serviceMethod,
                                String proposer, String recoverTime, String inspector) {
        SjqSolidliquidProduceFault sjqSolidliquidProduceFault = new SjqSolidliquidProduceFault();
        sjqSolidliquidProduceFault.setId(faultId);
        sjqSolidliquidProduceFault.setServiceTimeStart(serviceTimeStart);
        sjqSolidliquidProduceFault.setServiceTimeEnd(serviceTimeEnd);
        sjqSolidliquidProduceFault.setServicemanInCharge(servicemanInCharge);
        sjqSolidliquidProduceFault.setServiceman(serviceman);
        sjqSolidliquidProduceFault.setFaultReason(faultReason);
        sjqSolidliquidProduceFault.setServiceMethod(serviceMethod);
        sjqSolidliquidProduceFault.setProposer(proposer);
        sjqSolidliquidProduceFault.setRecoverTime(recoverTime);
        sjqSolidliquidProduceFault.setInspector(inspector);

        int res1 = sjqSolidliquidProduceFaultMapper.updateByPrimaryKeySelective(sjqSolidliquidProduceFault);
//        boolean res2 = sjqCompostProduceInspectionService.set_is_from(inspectionId);
        if (res1 == 1)
            return true;
        return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqSolidliquidProduceFault> list = sjqSolidliquidProduceFaultMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqSolidliquidProduceFault> list = sjqSolidliquidProduceFaultMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqSolidliquidProduceFaultMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqSolidliquidProduceFault record) {
        return sjqSolidliquidProduceFaultMapper.updateByPrimaryKeySelective(record);
    }
}
